﻿using UnityEngine;

[RequireComponent(typeof(MeshRenderer))]
[ExecuteInEditMode]
public class MeshAnimation : MonoBehaviour
{
    public Renderer meshRenderer = null;

    [System.NonSerialized] private Material sharedMaterial = null;

    public static int mainTexStId = Shader.PropertyToID("_MainTex_ST");
    public static int colorId = Shader.PropertyToID("_Color");
    public static int tintColorId = Shader.PropertyToID("_TintColor");
    public static int multiplierId = Shader.PropertyToID("_MMultiplier");
    public static int scrollXId = Shader.PropertyToID("_ScrollX");
    public static int scrollYId = Shader.PropertyToID("_ScrollY");
    public static int dissolveId = Shader.PropertyToID("_Dissolve");
    public static int cutoffID = Shader.PropertyToID("_Cutoff");
    
    public Vector4 _MainTex_ST;
    public Color _Color;
    public Color _TintColor;
    public float _MMultiplier;
    public float _ScrollX;
    public float _ScrollY;


    private bool isMainTexStEnabled;
    private bool isColorEnabled;
    private bool isTintColorEnabled;
    private bool isMultiplierEnabled;
    private bool isScrollXEnabled;
    private bool isScrollYEnabled;
    private bool isCutoffEnabled;

    public string SortingLayer;

    public int SortingOrder;
    
    [Header("溶解(0-1)")]public float _Cutoff;

    private void Start()
    {
        Initialize();

        sharedMaterial = Instantiate(meshRenderer.sharedMaterial);
        meshRenderer.sharedMaterial = sharedMaterial;
    }

    public void Initialize()
    {
        if (meshRenderer == null)
        {
            meshRenderer = gameObject.GetComponent<Renderer>();
        }

        SortingLayer = meshRenderer.sortingLayerName;
        SortingOrder = meshRenderer.sortingOrder;

        var material = meshRenderer.sharedMaterial;
        if (material == null)
        {
            UnityEngine.Debug.LogErrorFormat(gameObject, "material == null, {0}", name);
            return;
        }
        if (material.HasProperty("_MainTex_ST"))
        {
            isMainTexStEnabled = true;
            _MainTex_ST = material.GetVector(mainTexStId);
        }

        if (material.HasProperty("_Color"))
        {
            isColorEnabled = true;
            _Color = material.GetColor(colorId);
        }
        
        if (material.HasProperty("_TintColor"))
        {
            isTintColorEnabled = true;
            _TintColor = material.GetColor(tintColorId);
        }

        if (material.HasProperty("_MMultiplier"))
        {
            isMultiplierEnabled = true;
            _MMultiplier = material.GetFloat(multiplierId);
        }

        if (material.HasProperty("_ScrollX"))
        {
            isScrollXEnabled = true;
            _ScrollX = material.GetFloat(scrollXId);
        }

        if (material.HasProperty("_ScrollY"))
        {
            isScrollYEnabled = true;
            _ScrollY = material.GetFloat(scrollYId);
        }
        
        if (material.HasProperty("_Cutoff"))
        {
            isCutoffEnabled = true;
            _Cutoff = material.GetFloat(cutoffID);
        }
    }

    private void Update()
    {
        if (isMainTexStEnabled)
            sharedMaterial.SetVector(mainTexStId, _MainTex_ST);

        if (isColorEnabled)
            sharedMaterial.SetColor(colorId, _Color);

        if (isTintColorEnabled)
            sharedMaterial.SetColor(tintColorId, _TintColor);
        
        if (isMultiplierEnabled)
            sharedMaterial.SetFloat(multiplierId, _MMultiplier);

        if (isScrollXEnabled)
            sharedMaterial.SetFloat(scrollXId, _ScrollX);

        if (isScrollYEnabled)
            sharedMaterial.SetFloat(scrollYId, _ScrollY);

        if (isCutoffEnabled)
            sharedMaterial.SetFloat(cutoffID,_Cutoff);

#if UNITY_EDITOR
        meshRenderer.sortingLayerName = SortingLayer;
        meshRenderer.sortingOrder = SortingOrder;
#endif
    }

}
